package com.tanhua.mangement.controller;

import cn.hutool.core.util.ObjectUtil;
import com.tanhua.mangement.service.UserManageService;
import com.tanhua.mangement.vo.PageResult;
import com.tanhua.mangement.vo.UserManageBasicVo;
import com.tanhua.mangement.vo.UserManageFrozenVo;
import com.tanhua.mangement.vo.UserManageUnfrozenVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/manage")
@Slf4j
public class UserManageController {
    @Autowired
    private UserManageService userManageService;

    /**
     * 用户数据分页
     *
     * @param page
     * @param pageSize id	否
     *                 用户ID
     *                 nickname	否
     *                 昵称
     *                 city	否
     *                 注册城市名称，城市信息叶子节点名称即可，无需adcode
     * @return
     */
    //1.用户数据翻页
    @GetMapping("/users")
    public PageResult userManageDataPage(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                         @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize,
                                         @RequestParam(value = "id", required = false) Integer id,
                                         @RequestParam(value = "nickname", required = false) String nickname,
                                         @RequestParam(value = "city", required = false) String city) {
        PageResult pageResult = userManageService.userManageDataPage(page, pageSize,id,nickname,city);
        return pageResult;
    }

    //2.用户基本资料

    /**
     * 单个用户的基本资料
     *
     * @param userId 查询的id
     * @return
     */
    @GetMapping("/users/{userID}")
    public ResponseEntity<UserManageBasicVo> userManageBasic(@PathVariable("userID") String userId) {
        try {
            //调用方法
            UserManageBasicVo userManageBasicVo = this.userManageService.userManageBasic(userId);
            if (ObjectUtil.isNotEmpty(userManageBasicVo)) {
                return ResponseEntity.ok(userManageBasicVo);
            }
        } catch (Exception e) {
            log.error("查询用户基本资料出错= ", e);
        }
        return ResponseEntity.status(500).build();
    }

    //3.用户冻结操作
    @PostMapping("/users/freeze")
    public ResponseEntity<String> userFreeze(@RequestBody UserManageFrozenVo userManageFrozenVo) {
        try {
            //将接收到的信息传到service进行下一步的操作
            Boolean result = userManageService.userFreeze(userManageFrozenVo);
            if (result) {
                return ResponseEntity.ok("冻结成功");
            }
        } catch (Exception e) {
            log.error("冻结用户出错= ", e);
        }
        return ResponseEntity.status(500).body("用户冻结失败");
    }

    //4.用户解冻操作
    @PostMapping("/users/unfreeze")
    public ResponseEntity<String> userUnfreeze(@RequestBody UserManageUnfrozenVo userManageUnfrozenVo) {
        try {
            //将接收到的信息传到service进行下一步的操作
            Boolean result = userManageService.userUnfreeze(userManageUnfrozenVo);
            if (result) {
                return ResponseEntity.ok("解冻成功");
            }
        } catch (Exception e) {
            log.error("解冻用户出错= ", e);
        }
        return ResponseEntity.status(500).body("用户解冻失败");
    }
}
